package com.ziyia.fragment;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.ziyia.fragment.placeholder.JkTxFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "Activity" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button load = findViewById(R.id.load);
        Button reset1 = findViewById(R.id.reset1);
        Button reset2 = findViewById(R.id.reset2);
        Button tx = findViewById(R.id.tx);
        Button jktx2 = findViewById(R.id.jktx2);
        FrameLayout frameLayout = findViewById(R.id.frameLayout);

        
        load.setOnClickListener(this);
        reset1.setOnClickListener(this);
        reset2.setOnClickListener(this);
        tx.setOnClickListener(this);
        jktx2.setOnClickListener(this);


    }

    @Override
    protected void onStart() {
        Log.d(TAG, "onStart: ");
        super.onStart();
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume: ");
        super.onResume();
    }

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.load:
                replaceFragment(new ItemFragment());
                break;
            case R.id.reset1:
                replaceFragment(new BlankFragment());
                break;
            case R.id.reset2:
                replaceFragment(new BlankFragment2());
                break;
            case R.id.tx: // Bundle实现Activity和Fragment通信
                Bundle bundle = new Bundle();
                bundle.putString("name", "年少有为");
                TxFragment txFragment = new TxFragment();
                txFragment.setArguments(bundle);
                replaceFragment(txFragment);
                break;
            case R.id.jktx2: //
                JkTxFragment jkTxFragment = new JkTxFragment();
                setName("nsyw");
                replaceFragment(jkTxFragment);
                break;
        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frameLayout, fragment);
        // 每次都添加Fragment到栈中，如果栈不为空，点击返回键将返回上一个Fragment，否则退出Activity
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }


}